package org.nuplay;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import java.net.InetAddress;
import java.net.UnknownHostException;


import javax.swing.*;
import javax.swing.event.ChangeEvent;




public class NuPlayWiiMoteClient extends JFrame implements ActionListener, WindowListener   {
	

	private JComboBox audio_devices_choose;
	private JTextField ip_number_choose;
	private JComboBox channel;
	private JLabel error_label;

	private JButton connect_button;
	private boolean connected;
	private NuPlayWiiMoteDevice wiimote;
	private NuPlayWiiMoteSend sender;

	
	public NuPlayWiiMoteClient() {
		wiimote = new NuPlayWiiMoteDevice();
		connected = false;
		build_ui();
	}

	public void actionPerformed(ActionEvent arg0) {
		
		if (arg0.getSource() == connect_button) {
			if (connected) {
				disconnect();
			} else {
				connect();
			}
		}
	}
	
	private void build_ui() {
		addWindowListener(this);
		getContentPane().setLayout( new BorderLayout() );

		
		
		
		JPanel topPanel = new JPanel();
		topPanel.setLayout( null );
		getContentPane().add( topPanel, BorderLayout.CENTER );
		
		JLabel label_audio_device = new JLabel("Which WiiMote to use?");
		label_audio_device.setBounds(20, 20, 200, 20);
		audio_devices_choose = new JComboBox();
		audio_devices_choose.setBounds( 20, 40, 260, 20 );
		audio_devices_choose.addItem("wiimote 1");
		audio_devices_choose.addItem("wiimote 2");
		audio_devices_choose.addItem("wiimote 3");
		audio_devices_choose.addItem("wiimote 4");
		topPanel.add(label_audio_device);
		topPanel.add(audio_devices_choose);
	
		JLabel label_ip_address = new JLabel("Where is NuPlay running at?");
		label_ip_address.setBounds(20,70, 200, 20);
		
		ip_number_choose = new JTextField();
		ip_number_choose.setBounds( 20, 90, 260, 20 );
		try {
			ip_number_choose.setText(InetAddress.getLocalHost().getHostAddress());
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		
		topPanel.add(label_ip_address);
		topPanel.add(ip_number_choose);
		
		
		JLabel channel_text = new JLabel("Which channel to use?");
		channel_text.setBounds(20,120, 200, 20);
	
		
		channel = new JComboBox();
		for(int i=0; i < 32;i++) {
			channel.addItem(new Integer(i + 1));
		}
		channel.setBounds( 20, 140, 260, 20 );
		topPanel.add(channel_text);
		topPanel.add(channel);
		
	
		
	
		
		connect_button = new JButton("Connect to NuPlay.");
		connect_button.addActionListener(this);
		connect_button.setBounds( 20, 180, 200, 20 );
		topPanel.add(connect_button);
		
		error_label = new JLabel("ready...");
		error_label.setForeground(new Color(255,0,0));
		error_label.setBounds(20, 210,200,20);
		topPanel.add(error_label);

		java.net.URL imageURL = NuPlayWiiMoteClient.class.getResource("nuplay.png");
		if (imageURL != null) {
			ImageIcon icon = new ImageIcon(imageURL);
			JLabel image_label = new JLabel(icon);
			this.setIconImage(icon.getImage());
			image_label.setBounds( 240, 170, 50, 77 );
			topPanel.add(image_label);
		}
	}
	

	
	private void close_wiimote_device() {
		//
	}
	
	private void open_wiimote_device() {
		boolean is_connected = wiimote.connect(audio_devices_choose.getSelectedIndex() + 1);
		if (is_connected) {
			try {
				sender = new NuPlayWiiMoteSend((Integer)channel.getSelectedIndex(),wiimote, InetAddress.getByName(ip_number_choose.getText()), error_label);
			} catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} else {
			error_label.setText(audio_devices_choose.getSelectedItem() + " not found.");
			disconnect();
		}
	}
	
	
	private void disconnect() {
		connected = false;
		connect_button.setText("Connect to NuPlay.");
		close_wiimote_device();
		if (sender != null) {
			sender.close();
			sender = null;
		}
	}
	
	private void connect() {
		connected = true;
		connect_button.setText("Disconnect from NuPlay.");
		open_wiimote_device();
	}
	
	public void close() {
		System.exit(0);
	}

	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JFrame main_window = new NuPlayWiiMoteClient();
		main_window.setSize(320, 280);
		main_window.setTitle("NuPlay WiiMote client V1.0");
		main_window.setVisible(true);
	}

	public void windowActivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosed(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowClosing(WindowEvent e) {
		close();
		
	}

	public void windowDeactivated(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowDeiconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowIconified(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void windowOpened(WindowEvent e) {
		// TODO Auto-generated method stub
		
	}

	public void stateChanged(ChangeEvent e) {
		// TODO Auto-generated method stub
	}


	
}
